﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communication;
using Database;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public class ClientCommunication : IClientCommunication, IDisposable
    {
        IServerCommunication serverCommunication = null;
        ForumService forumService = null;
        int forum;


        public ClientCommunication(ForumService fs, int forum)
        {
            this.forumService = fs;
            InitCommunication();
            this.forum = forum;
            //probably need to add a call to enter a certain forum here
        }

        public void InitCommunication()
        {
            DuplexChannelFactory<IServerCommunication> communicationFactory =
                  new DuplexChannelFactory<IServerCommunication>(
                      new InstanceContext(this),
                      new NetTcpBinding(),
                      new EndpointAddress("net.tcp://localhost:8000/ISubscribe"));
            try
            {
                serverCommunication = communicationFactory.CreateChannel();
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Couldn't establish a connection");
            }
        }

        //callbacks

        public void ReceiveJoin(Database.User client, bool ans, string systemMsg)
        {
            if (!ans)
            {
                new FormError("Illegal Username OR Password!").Show();
            }
            else
            {
                this.forumService.receiveJoin(client);
            }
        }

        public void ReceiveLogout()
        {
        }

        public void ReceiveNotify(string notifyMsg)
        {
            this.forumService.receiveUpdateList(notifyMsg);
        }

        public void ReceiveUsersList(List<User> uList)
        {
            this.forumService.receiveUsersList(uList);
        }

        public void AutherizationError()
        {
            this.forumService.autherizationError();
        }

        public void ReceiveForum(Database.Forum forum)
        {
            this.forumService.receiveForum(forum);
        }

        public void ReceiveSubForum(Database.SubForum subforum)
        {
            this.forumService.receiveSubForum(subforum);
        }

        public void ReceiveThread(Database.Thread thread)
        {
            this.forumService.receiveThread(thread);
        }

        public void ReceiveSystemStatistics(string stats, Database.User me)
        {
            this.forumService.receiveAdminAccess(stats, me);
        }

        public void ReceiveForumStatistics(string stats)
        {
            this.forumService.ReceiveForumStatistics(stats);
        }


        //server functions


        internal void logout(Database.User me)
        {
            try
            {
                this.serverCommunication.Logout(me, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }


        internal void getUserList(Database.User me)
        {
            try{
                this.serverCommunication.getForumUserList(me, forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void getMainForum(Database.User me)
        {
            try{
              this.serverCommunication.GetForum(new ClientRequest(me,this.forum));
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void getSubForum(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.GetSubForum(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void getThread(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.GetThread(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void promote(Database.User me, string name, string forum)
        {
            try
            {
                this.serverCommunication.AddModerator(me, name, forum, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void AddForum(Database.User me, Database.User name, string forum , int mods, int modCan, int modsDel, int notific, int mailAu, List<string> badWords)
        {
            try
            {
                this.serverCommunication.AddForum(me, name, forum, mods, modCan, modsDel, notific, mailAu, badWords);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void register(string name, string last, string nick, string pass, string email)
        {
            try
            {
                this.serverCommunication.Register(name, last, nick, pass, email, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void login(string flag, string user, Byte[] password)
        {
            try
            {
                this.serverCommunication.Login(flag, user, password, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void postReply(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.AddPost(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void newThread(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.AddThread(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void newSub(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.AddSubForum(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void exchangeModerator(Database.User me, string oldMod, string newMod, string forum)
        {
            try
            {
                this.serverCommunication.ExchangeModerator(me, oldMod, newMod, forum, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }


        internal void deleteThread(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.DeleteThread(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }


        internal void deletePost(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.DeletePost(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void demote(Database.User me, string name, string forum)
        {
            try
            {
                this.serverCommunication.DeleteModerator(me, name, forum, this.forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void deleteSubForum(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.DeleteSubForum(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void getForumStats(Database.User me)
        {
            try
            {
                this.serverCommunication.GetForumStatistics(me, forum);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void getForumsStats(string user, string password)
        {
            try
            {
                this.serverCommunication.GetSystemStatistics(user, password);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void editThread(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.EditThread(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void editPost(ClientRequest clientRequest)
        {
            try
            {
                this.serverCommunication.EditPost(clientRequest);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }


        internal void adminExit(Database.User me)
        {
            try
            {
                this.serverCommunication.AdminExit(me);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }

        internal void confirmCode(string name, string last, string nick, string pass, string email, string text)
        {
            try
            {
                this.serverCommunication.ConfirmMailForRegister(name, last, nick, pass, email, this.forum, text);
            }
            catch
            {
                new FormError("Disconnected from the server, please try to restart the Client").Show();
            }
        }


        public void Dispose()
        {
            this.Dispose();
        }


        public void ReceiveForumUsersList(List<User> uList)
        {
            this.forumService.receiveUsersList(uList);
        }

        public void ReceiveSystemUsersList(List<string> uList)
        {
            throw new NotImplementedException();
        }

        public void RecieveError(string content)
        {
            this.forumService.RecieveError(content);
        }




        

        public void RecieveCodeConfirm()
        {
            this.forumService.recieveCodeConfirm();
        }
    }
}
